﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.PeerToPeer;

namespace Resolve
{
    class ResolveProgram
    {
        static PeerNameRecordCollection records;

        static void Main(string[] args)
        {

            Console.WriteLine("Peer Classifer to Resolve: ");
            string classifer = Console.ReadLine();

            PeerNameResolver resolve = new PeerNameResolver();

            Console.WriteLine("Please wait resolving....");


            try
            {

                records = resolve.Resolve(new PeerName(classifer,PeerNameType.Unsecured));
                DisplayResults();

            }
            catch(PeerToPeerException ex){

                Console.WriteLine("Exception caught: {0}", ex.Message);
            }

            Console.WriteLine();
            Console.WriteLine("Please press enter to exit");
            Console.ReadLine();

        }

        private static void DisplayResults()
        {
            if (records.Count > 0)
            {
                UnicodeEncoding encoding = new UnicodeEncoding();
                Console.WriteLine();
                Console.WriteLine();

                foreach (var record in records)
                {
                    string data = (record.Data != null) ? encoding.GetString(record.Data) : string.Empty;

                    Console.WriteLine("*******Peer: {0}\n\r\tHost{1}\n\r\tComment: {2}\n\r\tData: {3} ******* ", record.PeerName.PeerHostName, record.PeerName.PeerHostName, record.Comment, record.Data);

                    foreach (var endpoint in record.EndPointCollection)
                    {
                        Console.WriteLine("/tEndpoint: {0}, Port: {1}", endpoint.Address.ToString(), endpoint.Port);
                        Console.WriteLine();
                    }


                }

            }



        }
    }
}
